package com.dhxt.springcloud;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
/**
 * @Description: 微服务整合了jpa功能
 * @author hp
 * @date 2017年7月16日 上午9:47:23
 */
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient // 启动EnableEureka客户端
public class SpringCloudTestApplication {

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		// 配置访问微服务账号密码
		List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
		list.add(new BasicAuthorizationInterceptor("hp", "hp"));
		restTemplate.setInterceptors(list);
		
		// 配置字符集
		List<HttpMessageConverter<?>> msgList = restTemplate.getMessageConverters();
		for (HttpMessageConverter<?> converter : msgList) {
			if (converter instanceof StringHttpMessageConverter) {
				((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
				break;
			}
		}
		
		return restTemplate;
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SpringCloudTestApplication.class, args);
	}

}